/********************************************************************
	created:	2012/08/02
	created:	2:8:2012   10:31
	filename: 	tiny_tls.h
	file path:	
	file base:	tiny_tls
	file ext:	h
	author:		cg
	
	purpose:	
*********************************************************************/
#pragma once

#ifndef HANDLE
	#define HANDLE void*
#endif

//#define _USE_TLS_LOCK

class CTinyTLS
{
public:
	CTinyTLS(){}
	virtual ~CTinyTLS(){}

public:
	HANDLE	GetHandler()
	{
		unsigned int nCurThreadID = RtGetCurrentThreadId();
		
		HANDLE hHandler = NULL;
#ifdef _USE_TLS_LOCK
		m_lock.Lock();
#endif
		map<unsigned int, HANDLE>::iterator it;
		it = m_mapHandlers.find(nCurThreadID);
		if(it != m_mapHandlers.end())
		{
			hHandler = it->second;
		}
#ifdef _USE_TLS_LOCK
#ifdef __KTSDK__
		m_lock.Unlock();
#else
		m_lock.UnLock();
#endif
#endif
		return hHandler;
	}
	void	SetHandler(HANDLE hHandler)
	{
		unsigned int nCurThreadID = RtGetCurrentThreadId();
#ifdef _USE_TLS_LOCK
		m_lock.Lock();
#endif
		m_mapHandlers.insert(std::make_pair(nCurThreadID, hHandler));

#ifdef _USE_TLS_LOCK
#ifdef __KTSDK__
		m_lock.Unlock();
#else
		m_lock.UnLock();
#endif
#endif
	}

private:
	map<unsigned int, HANDLE>	m_mapHandlers;
#ifdef _USE_TLS_LOCK
#ifdef __KTSDK__
	TCriticalSection			m_lock;
#else
	CRtMutexThreadRecursive		m_lock;
#endif
#endif
};






